<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      @row-del="rowDel"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
    </avue-crud>
  </basic-container>
</template>

<script>
import { add, getList, remove, update } from "@/api/article/categories";
import website from "@/config/website";
import { getDeptTree } from "@/api/system/dept";
import { getRoleTree } from "@/api/system/role";
import { getPostList } from "@/api/system/post";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      selectionList: [],
      loading: true,
      query: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        border: true,
        align: "left",
        menuAlign: "center",
        columnBtn: false,
        refreshBtn: false,
        column: [
          {
            label: "序号",
            prop: "sort",
            type: "number",
            rules: [
              {
                required: true,
                message: "请输入排序",
                trigger: "blur",
              },
            ],
          },
          {
            label: "分类名称",
            prop: "name",
            rules: [
              {
                required: true,
                message: "请输入分类名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "显示",
            prop: "status",
            type: "select",
            dicData: [
              {
                label: "显示",
                value: 1,
              },
              {
                label: "不显示",
                value: 0,
              },
            ],
            rules: [
              {
                required: true,
                message: "请选择是否展示",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
      props: {
        label: "title",
        value: "key",
      },
    };
  },
  computed: {},
  methods: {
    rowSave(row, done, loading) {
      //新增
      add(row).then(
        () => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      //修改
      update(row).then(
        () => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      //删除
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    currentChange(currentPage) {
      //分页页码
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      //分页条数
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      //调用接口初始化
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        this.data = res.data.data.records;
        console.log(this.data);
        this.loading = false;
      });
    },
  },
};
</script>

<style>
</style>
